﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OPT_TYPE
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
    <table id="mas" style="display: none">
    </table>
    <div id="dialog" title="Add / Edit">
        <table>
        <tr><td colspan="2">&nbsp;</td></tr>
            <tr>
                <td>
                    Code
                </td>
                <td>
                    &nbsp;
                    <input id="txtcode" maxlength="2" />
                </td>
            </tr>
            <tr>
                <td>
                    Name
                </td>
                <td>
                    &nbsp;
                    <input id="txtType" maxlength="50" />
                </td>
            </tr>
        </table>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">

</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server">
    <% if (false)
       { %>
    <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <% } %>
    <script type="text/jscript">
        $(function () {

            
            $("#mas").flexigrid({
                url: '<%= Url.Action("OPT_TYPE_List") %>',
                dataType: 'json',
                colModel: [
				{ display: 'Code', name: '_Code', width: 100, sortable: true, align: 'left' },
				{ display: 'Type', name: '_Type', width: 250, sortable: true, align: 'left' }
				],
                buttons: [
                { name: 'Add', bclass: 'add', onpress: Adddata },
                   { separator: true },
                { name: 'Edit', bclass: 'edit', onpress: Editdata }

                ],
                sortname: '_Code',
                sortorder: 'asc',
                usepager: true,
                rp: 15,
                width: 900,
                height: 380, singleSelect: true,title:"OPTION TYPE"
            });

            function Adddata() {

                setTimeout(function () { $("#txtcode").focus(); }, 1000);
                $("#txtType").val("");
                $("#dialog").dialog("open");
            }
            function Editdata() {
                var row = ($("#mas").find(".trSelected").length);
                if (row <= 0) {
                    $().toastmessage('showErrorToast', "Please Select Data Edit");
                    return;
                }
                row = $("#mas .trSelected");
                var code = row.find("td:first").text();
                var types = row.find("td").eq(1).text();
                var notuse = row.find("td").eq(2).text().length;
                $("#txtcode").val(code).attr("disabled", "disabled");
                $("#txtType").val(types);
                $("#dialog").dialog("open");
            }

            $("#dialog").on("dialogclose", function (event, ui) {                            
                $("#txtcode").val("").removeAttr("disabled");
            });
            $("#dialog").dialog({
                width: 350,
                height: 250,
                autoOpen: false, position: [500, 150],
                open: function (event, ui) {
                    if ($("#txtcode").is("[disabled]")) {
                        setTimeout(function () { $("#txtType").focus(); }, 1000);
                    } else {
                        setTimeout(function () { $("#txtcode").focus(); }, 1000);
                    }

                },
                close: function (event, ui) {
                   
                },
                show: "blind",
                buttons: {
                    "Save": function () {
                        if ($("#txtcode").val() == "") {
                            $().toastmessage('showErrorToast', 'Pleas Input Code.');
                            setTimeout(function () { $("#txtcode").focus(); }, 1000);
                            return;
                        }
                        if ($("#txtType").val() == "") {
                            $().toastmessage('showErrorToast', 'Pleas Input Name');
                            $("#txtType").focus();
                            return;
                        }

                        var dataForSubmit = new Array();
                        dataForSubmit.push({
                            _Code: $("#txtcode").val(),
                            _Type: $("#txtType").val()
                        });

                        var getUsersUrl = '<%= Url.Action("Add_opttype") %>';
                        if ($("#txtcode").is("[disabled]")) {
                            getUsersUrl = '<%= Url.Action("Edit_opttype") %>';
                        }
                        callScriptMethod(getUsersUrl, { theData: dataForSubmit });


                    },
                    "Cancel": function () {
                        $("#dialog").dialog("close");
                    }
                }
            });

            function callScriptMethod(url, jsonObject, action, callback, async) {

                callback = callback || function () { };
                async = (async == null || async);

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: url,
                    data: JSON.stringify(jsonObject),
                    dataType: "json",
                    async: async,
                    success: function (data) {

                        if (data.success == "1") {
                            $().toastmessage('showSuccessToast', data.message);
                            $("#mas").flexgridReload('<%= Url.Action("OPT_TYPE_List") %>')
                            $("#dialog").dialog("close");
                        } else {
                            $().toastmessage('showErrorToast', data.message);

                        }
                    },
                    error: function (xhr, status, error) {
                        var err = xhr.responseText;
                        alert(err.Message);
                    }
                });
            }
        });
    </script>
</asp:Content>